package com.gitee.poi.plugin.core.handler;

import com.gitee.poi.plugin.annotations.core.Align;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Sheet;

import java.lang.reflect.Field;

public class AlignProcessor extends AbstractProcessor implements HandlerAnnotation {

    private Class supportAnnotation = Align.class;

    public CellStyle process(Font font, CellStyle style, Sheet sheet, Field field) {
        Align align = field.getDeclaringClass().getAnnotation(Align.class);
        style = setStyle(font, style, align);
        align = field.getAnnotation(Align.class);
        style = setStyle(font, style, align);
        return style;
    }

    private CellStyle setStyle(Font font, CellStyle style, Align align) {
        if (align != null) {
            HorizontalAlignment alignment = align.value();
            style.setAlignment(alignment);
        }
        return style;
    }

    public Class getSupportAnnotation() {
        return supportAnnotation;
    }

    @Override
    public boolean supportsAnnotation(Field field) {
        return super.supportsAnnotation(field) || field.getDeclaringClass().getAnnotation(supportAnnotation) != null;
    }
}
